package com.probox.modules.controller.customer;

import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.customer.CustomerAddress;
import com.probox.common.entity.service.customer.ICustomerAddressService;
import com.probox.common.entity.vo.customer.CustomerAddressVo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 客户收货地址Controller
 *
 * @author zsy
 * @date 2021-06-18
 */
@RestController
@RequestMapping("/customer/address")
public class CustomerAddressController extends BaseController {
    @Autowired
    private ICustomerAddressService customerAddressService;

    /**
     * 查询客户收货地址列表
     */
    @PreAuthorize(hasPermi = "customerAddress:address:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerAddressVo customerAddress) {
        startPage();
        List<CustomerAddressVo> list = customerAddressService.queryListvo(customerAddress);
        return getDataTable(list);
    }

    /**
     * 查询指定客户收货地址列表
     */
    @PreAuthorize(hasPermi = "customerAddress:address:list")
    @GetMapping("/getByIdAddress/{customerUid}")
    public TableDataInfo getByIdAddress(@PathVariable("customerUid") Long customerUid) {
        startPage();
        List<CustomerAddressVo> list = customerAddressService.getByIdAddress(customerUid);
        return getDataTable(list);
    }

    /**
     * 导出客户收货地址列表
     */
    @PreAuthorize(hasPermi = "customerAddress:address:export")
    @Log(title = "客户收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerAddressVo customerAddress) throws IOException {
        List<CustomerAddressVo> list = customerAddressService.queryListvo(customerAddress);
        ExcelUtil<CustomerAddressVo> util = new ExcelUtil<CustomerAddressVo>(CustomerAddressVo.class);
        util.exportExcel(response, list, "客户收货地址数据");
    }

    /**
     * 获取客户收货地址详细信息
     */
    @PreAuthorize(hasPermi = "customerAddress:address:query")
    @GetMapping(value = "/{addressUid}")
    public AjaxResult getInfo(@PathVariable("addressUid") Long addressUid) {
        return AjaxResult.success(customerAddressService.selectCustomerAddressDetail(addressUid));
    }

    /**
     * 新增客户收货地址
     */
    @PreAuthorize(hasPermi = "customerAddress:address:add")
    @Log(title = "客户收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerAddress customerAddress) {
        return toAjax(customerAddressService.insertCustomerAddress(customerAddress));
    }

    /**
     * 修改客户收货地址
     */
    @PreAuthorize(hasPermi = "customerAddress:address:edit")
    @Log(title = "客户收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerAddress customerAddress) {
        return toAjax(customerAddressService.updateCustomerAddress(customerAddress));
    }

    /**
     * 删除客户收货地址
     */
    @PreAuthorize(hasPermi = "customerAddress:address:remove")
    @Log(title = "客户收货地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{addressUids}")
    public AjaxResult remove(@PathVariable Long[] addressUids) {
        return toAjax(customerAddressService.deleteCustomerAddressByIds(addressUids));
    }


    /**
     * 修改地址状态信息
     */
    @ApiOperation(value="修改地址状态信息")
    @PreAuthorize(hasPermi = "customerAddress:address:edit")
    @Log(title = "修改地址状态信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateAddressStatus")
    public AjaxResult updateAddressStatus(@RequestBody CustomerAddress customerAddress) {
        return toAjax(customerAddressService.updateAddressStatus(customerAddress));
    }
}
